import java.awt.MouseInfo;

import com.melloware.jintellitype.IntellitypeListener;
import com.melloware.jintellitype.JIntellitype;

public class MyHotkeyListener implements
		com.melloware.jintellitype.HotkeyListener, IntellitypeListener
{
	public static final int WIN_C = 1;
	public static final int WIN_V = 2;
	public static final int CTRL_SHIFT_Q = 3;
	public static final int WIN_ALT_X = 4;
	public static final int WIN_ALT_F = 5;

	boolean exitKeyPressed = false;

	TextTransfer ttf;
	PasteInvoker pinv;
	TransparentPopupMenu tpm;
	ClipboardHinstory cbh;
	ClipboardFavorites cbf;

	MyHotkeyListener(TextTransfer ttf, PasteInvoker pinv,
			TransparentPopupMenu tpm, ClipboardHinstory cbh,
			ClipboardFavorites cbf)
	{
		this.ttf = ttf;
		this.pinv = pinv;
		this.tpm = tpm;
		this.cbh = cbh;
		this.cbf = cbf;

		// Initialize JIntellitype
		JIntellitype.getInstance();

		// OPTIONAL: check to see if an instance of this application is already
		// running, use the name of the window title of this JFrame for checking
		// if (JIntellitype.checkInstanceAlreadyRunning("MyApp")) {
		// LOG.error("An instance of this application is already running");
		// System.exit(1);
		// }

		// Assign global hotkeys.
		JIntellitype.getInstance().registerHotKey(WIN_C, JIntellitype.MOD_WIN,
				(int) 'C');
		JIntellitype.getInstance().registerHotKey(WIN_V, JIntellitype.MOD_WIN,
				(int) 'V');
		JIntellitype.getInstance().registerHotKey(CTRL_SHIFT_Q,
				JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT, (int) 'Q');
		JIntellitype.getInstance().registerHotKey(WIN_ALT_X,
				JIntellitype.MOD_WIN + JIntellitype.MOD_ALT, (int) 'X');
		JIntellitype.getInstance().registerHotKey(WIN_ALT_F,
				JIntellitype.MOD_WIN + JIntellitype.MOD_ALT, (int) 'F');
		// or you can use the Swing constants instead
		// JIntellitype.getInstance().registerSwingHotKey(3, Event.CTRL_MASK +
		// Event.SHIFT_MASK, (int)'C');

		// To unregister them just call unregisterHotKey with the unique
		// identifier
		// JIntellitype.getInstance().unregisterHotKey(1);
		// JIntellitype.getInstance().unregisterHotKey(2);
		// JIntellitype.getInstance().unregisterHotKey(3);

	}

	@Override
	public void onIntellitype(int command)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onHotKey(int identifier)
	{
		System.out.println(identifier);
		switch (identifier) {
		case WIN_C:
			ttf.setClipboardContents(cbh.prev());
			break;
		case WIN_V:
			ttf.setClipboardContents(cbh.next());
			break;
		case CTRL_SHIFT_Q:
			exitKeyPressed = true;
			break;
		case WIN_ALT_X:
			if (tpm.isVisible()) {
				tpm.setLocation(MouseInfo.getPointerInfo().getLocation());
			}
			tpm.setVisible(!tpm.isVisible());
			break;
		case WIN_ALT_F:
			cbf.add(ttf.getClipboardContents());
			break;
		default:
			break;
		}

	}

	// listen for hotkey
	// public void onHotKey(int aIdentifier) {
	// if (aIdentifier == 1)
	// System.out.println("WINDOWS+A hotkey pressed");
	// }

	// // listen for intellitype play/pause command
	// public void onIntellitype(int aCommand) {
	// switch (aCommand) {
	// case JIntellitype.APPCOMMAND_MEDIA_PLAY_PAUSE:
	// System.out.println("Play/Pause message received " +
	// Integer.toString(aCommand));
	// break;
	// default:
	// System.out.println("Undefined INTELLITYPE message caught " +
	// Integer.toString(aCommand));
	// break;
	// }
	// }
}
